package jm.data.structure.queue;


import java.util.Stack;

/**
 * @Description 用栈实现队列，对应力扣：https://leetcode-cn.com/problems/implement-queue-using-stacks/
 * @date 2022/4/8 8:46
 */
public class StackQueue {

    private Stack<Integer> inStack;
    private Stack<Integer> outStack;

    public StackQueue() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }


    /**
     * 入队
     * @param x
     */
    public void push(int x) {
        inStack.push(x);
    }

    /**
     * 出队
     * @return
     */
    public int pop() {
        checkStack();
        return outStack.pop();
    }

    /**
     * 查看队头元素
     * @return
     */
    public int peek() {
        checkStack();
        return outStack.peek();
    }

    /**
     * 队列是否为空
     * @return
     */
    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }

    /**
     * 检查outStack是否有元素。
     */
    private void checkStack(){
        if (outStack.isEmpty()){
            while (!inStack.isEmpty()){
                outStack.push(inStack.pop());
            }
        }
    }
}
